//
//  AddCompanyMembersVC.m
//  NtoccShipper
//
//  Created by mac on 2020/1/9.
//  Copyright © 2020 zhuchongjin. All rights reserved.
//

#import "AddCompanyMembersVC.h"
@interface AddCompanyMembersVC ()<UITableViewDelegate,UITableViewDataSource >
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) CJTextField *fieldName;
@property (nonatomic,strong) CJTextField *fieldPhone;
@property (nonatomic,strong) CJTextField *fieldPassWord;
@property (nonatomic,strong) CJTextField *fieldType;
@property (nonatomic,copy) NSString *strType;
@property (nonatomic,copy) NSString *strRoleCode;
@property (nonatomic,strong) FlexTouchView *touchFieldType;

@property (nonatomic,strong) UIImageView *imgVLeft;
@end

@implementation AddCompanyMembersVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setBackLeftButton:@""];
    [self.view addSubview:self.tableView];
    [self createTableView];
  
    if (self.isChange) {
        [self setTitleName:CJLString(@"修改公司成员")];
    }else{
        [self setTitleName:CJLString(@"新增公司成员")];

    }
    [self setFieldLimit];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (self.isChange) {
        [self loadDataAndUI];
        self.touchFieldType.userInteractionEnabled = NO;
    }else{
        self.touchFieldType.userInteractionEnabled = YES;
    }
}
- (void)loadDataAndUI{
    
    self.fieldName.text = self.model.userName;
    self.fieldPhone.text = self.model.phone;
    self.fieldPassWord.text = @"";
    self.strType = self.strRole;
    self.fieldType.text = self.strRoleName;
    
}
/// 创建tableview 等
- (void)createTableView{

    CGRect rcFrame = CGRectMake(0, kTopHeight, [[UIScreen mainScreen] bounds].size.width, 0);
    FlexFrameView *header = [[FlexFrameView alloc] initWithFlex:@"AddCompanyMembersHeaderView" Frame:rcFrame Owner:self];
    if (self.isChange) {
        self.imgVLeft.hidden = YES;
    }else{
        self.imgVLeft.hidden = NO;
        CompanyMembersModel *model = self.listModel[0];
        self.strType = model.role;
        self.fieldType.text = model.roleName;
        self.strRoleCode = model.code;
    }

    header.flexibleHeight = YES;
    [header layoutIfNeeded];
    self.tableView.tableHeaderView = header;
    [self.tableView reloadData];
}

- (void)setFieldLimit{
    self.fieldName.textFieldType = CJTextFieldStringTypeNone;
    self.fieldName.maxLength = CJTextLengthSixtyFour;
    self.fieldPhone.textFieldType = CJTextFieldStringTypeNone;
    self.fieldPhone.maxLength = CJTextLengthPhone;
}

- (void)btnSaveInfoAction{
    
    if ([CJRegular getIsStrEmpty:self.fieldName.text]) {
        [CJAlert showStringDismissWithDelay:@"请填写员工姓名"];
    }else if([CJRegular getIsStrEmpty:self.fieldPhone.text]){
        [CJAlert showStringDismissWithDelay:@"请填写正确的手机号码"];
    }else{
        if ([CJRegular getIsStrEmpty:self.fieldPassWord.text]) {
            self.fieldPassWord.text = @"";
        }
        
        NSString *strUrl  = CJStringWithFormat(@"%@/ntocc-basic-api/shipment/staff",CJBaseUrl);
        NSDictionary *dicPar;
        
        WS(ws);
        if (self.isChange) {
            dicPar = @{
                @"userName":self.fieldName.text,
                @"upass":self.fieldPassWord.text,
                @"phone":self.fieldPhone.text,
                @"shipmentRoleType":self.strType,
                @"code":self.model.code
            };
            
            [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PutType url:strUrl parameters:dicPar status:OperationTip success:^(id  _Nonnull responseObject) {
                if (ws.blockChange) {
                    [ws pop];
                }
            } failure:^(NSError * _Nonnull error) {
                
            }];
        }else{
            
            dicPar = @{
                @"userName":self.fieldName.text,
                @"upass":self.fieldPassWord.text,
                @"phone":self.fieldPhone.text,
                @"shipmentRoleType":self.strType,
                @"roleCode":self.strRoleCode,
            };
            
            [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PostType url:strUrl parameters:dicPar status:OperationTip success:^(id  _Nonnull responseObject) {
                CJSendNotify(CJCompanyMembersRefresh, nil)
                [ws pop];
            } failure:^(NSError * _Nonnull error) {
                
            }];
        }
    }
}

- (void)selectCompanyMembers{
    
    //   [@"发货人",@"财务",@"收货人",@"上游客户" ,@"代付机构"];
    // 23456
    if (self.isChange) {
        return;
    }
    
    [self.view endEditing:NO];
    
    NSMutableArray *listSource = [[NSMutableArray alloc]init];
    NSMutableArray *listType = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < self.listModel.count; i++) {
        CompanyMembersModel *model = self.listModel[i];
        [listSource addObject:model.roleName];
        [listType addObject:model.role];
    }
    
    
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]initWithPickerMode:BRStringPickerComponentSingle];
     stringPickerView.title = @"选择员工角色";
     stringPickerView.dataSourceArr = listSource;
    NSArray *arrList = listType;
     WS(ws);
     stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {
         ws.fieldType.text = resultModel.selectValue;
         ws.strType = arrList[resultModel.index];
         CompanyMembersModel *mod = ws.listModel[resultModel.index];
         ws.strRoleCode = mod.code;
     };

     BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:[UIColor cjMainColor]];
     stringPickerView.pickerStyle = customStyle;
     [stringPickerView show];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *MeViewCellId = @"DriverAouthVCcellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MeViewCellId];
    if (cell ==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MeViewCellId];
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    return cell;
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        ;
    }
    return _tableView;
}

@end
